import { _decorator, Component, Node, EventHandler } from 'cc';
import { KeyEventListener } from '../../core/GameInputListeners';
import { InputKeyCode } from '../../core/InputConfig';
import GameInputEvent from '../../core/GameInputEvent';
import GameInputMgr from '../../core/GameInputMgr';


const { ccclass, property } = _decorator;
/**
 * 任意键处理
 */
@ccclass('AnyKeyCodeHandler')
export class AnyKeyCodeHandler extends Component implements KeyEventListener {

    @property(EventHandler)
    handler: EventHandler = new EventHandler();

    public get isActive() {
        // LogUtils.log('KeyCodeHandler  isActive ', this.node.activeInHierarchy, this.activeInHierarchy)
        return this.node.activeInHierarchy;
    }
    isBlockInput(): boolean {
        return false;
    }

    onKeyBoardPress(keyCode: InputKeyCode, event?: GameInputEvent): void {
        if (keyCode < InputKeyCode.MOUSE_LEFT || keyCode == undefined) {
            if (!this.isActive) {
                return;
            }
            if (this.handler) {
                this.handler.emit([keyCode, event])
            }
        }
    }
    onKeyBoardRelease(keyCode: InputKeyCode, event?: GameInputEvent): void {

    }

    onEnable() {
        GameInputMgr.instance().addAnyKeyEventListener(this);
    }

    onDisable() {
        GameInputMgr.instance().removeAnyKeyEventListener(this);
    }
}

